package com.lagou.service.impl;

import com.lagou.base.StatusCode;
import com.lagou.dao.CourseContentDao;
import com.lagou.dao.impl.CourseContentDaoImpl;
import com.lagou.pojo.Course;
import com.lagou.pojo.Course_Lesson;
import com.lagou.pojo.Course_Section;
import com.lagou.service.CourseContentService;
import com.lagou.utils.DateUtils;

import java.util.List;

/**
 * @description: 课程内容 Service层接口 实现类
 * @author: zhuxiang
 * @create: 2020-07-28 14:02
 **/
public class CourseContentServiceImpl implements CourseContentService {
    //创建contentDao对象
    CourseContentDao contentDao = new CourseContentDaoImpl();

    //根据课程id查询课程内容
    @Override
    public List<Course_Section> findSectionAndLessonByCourseId(int courseId) {

        List<Course_Section> sectionList = contentDao.findSectionAndLessonByCourseId(courseId);
        return sectionList;
    }

    //根据id查询课程信息
    @Override
    public Course findCourseByCourseId(int courseId) {
        Course course = contentDao.findCourseByCourseId(courseId);
        return course;
    }

    //保存章节信息
    @Override
    public String saveSection(Course_Section section) {

        //1.补全章节信息
        section.setStatus(2);
        String format = DateUtils.getDateFormart();
        section.setCreate_time(format);
        section.setUpdate_time(format);

        //2.调用dao
        int row = contentDao.saveSection(section);

        //3.根据是否插入成功，封装对应信息
        if (row > 0){
            //插入成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //修改章节信息
    @Override
    public String updateSection(Course_Section section) {

        //1.补全信息
        String format = DateUtils.getDateFormart();
        section.setUpdate_time(format);

        //2.调用dao
        int row = contentDao.updateSection(section);
        //判断是否保存成功
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //修改章节状态
    @Override
    public String updateSectionStatus(int id, int status) {

        int row = contentDao.updateSectionStatus(id, status);
        //判断是否保存成功
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //保存课时信息
    @Override
    public String saveLesson(Course_Lesson lesson) {

        //补全信息
        String format = DateUtils.getDateFormart();
        lesson.setCreate_time(format);
        lesson.setUpdate_time(format);
        int row = contentDao.saveLesson(lesson);
        //判断是否保存成功
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }

    //编辑课时信息
    @Override
    public String updateLesson(Course_Lesson lesson) {

        //补全信息
        String format = DateUtils.getDateFormart();
        lesson.setUpdate_time(format);
        int row = contentDao.updateLesson(lesson);
        //判断是否保存成功
        if (row > 0){
            //保存成功
            String result = StatusCode.SUCCESS.toString();
            return result;
        }else {
            //保存失败
            String result = StatusCode.FAIL.toString();
            return result;
        }
    }
}
